package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.StudyResource;
import com.cyz.common.bean.StudyResourceTypeId;
import com.cyz.common.mapper.StudyResourceTypeIdMapper;
import com.cyz.user.userService.StudyResourceService;
import com.cyz.user.userService.StudyResourceTypeIdService;
import com.cyz.user.userService.StudyResourceTypeService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.user.vo.StudyResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 34928
* @description 针对表【study_resource_type_id】的数据库操作Service实现
* @createDate 2023-02-22 19:51:54
*/
@Service
public class StudyResourceTypeIdServiceImpl extends ServiceImpl<StudyResourceTypeIdMapper, StudyResourceTypeId>
    implements StudyResourceTypeIdService {

    @Autowired
    private StudyResourceService studyResourceService;
    @Autowired
    private StudyResourceTypeService studyResourceTypeService;

    @Override
    public List<StudyResourceVo> getStudyResourcesByTypeId(Integer id) {
        //创建筛选器
        LambdaQueryWrapper<StudyResourceTypeId> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据分类id 查询按钮
        lambdaQueryWrapper.eq(StudyResourceTypeId::getReosurceTypeId,id);
        //使用stream流处理逻辑
        List<StudyResource> collect = list(lambdaQueryWrapper).stream()
                //转换为按钮id
                .map(StudyResourceTypeId::getResourceId)
                //转换为按钮
                .map(item -> studyResourceService.getById(item))
                //转换为list
                .collect(Collectors.toList());
        //vo处理
        return BeanCopyUtil.copyBeanList(collect, StudyResourceVo.class);
    }

    @Override
    public List<String> getTypesByResourceId(Integer id) {
        LambdaQueryWrapper<StudyResourceTypeId> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<String> types = new ArrayList<>();
        lambdaQueryWrapper.eq(StudyResourceTypeId::getResourceId,id);
        list(lambdaQueryWrapper).stream()
                .map(StudyResourceTypeId::getReosurceTypeId)
                .forEach(item -> {
                    types.add(studyResourceTypeService.getById(item).getName());
                });
        return types;
    }
}




